package cn.zhuama.basic.common;

import cn.zhuama.basic.util.StrUtils;
import cn.zhuama.basic.util.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.TimeUnit;
@RestController
@RequestMapping("/common")
public class VerifyCodeController {
    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("/getimage/{redisKey}")
    public  String getimage(@PathVariable("redisKey") String redisKey, HttpServletResponse response)throws IOException {
    String randomCode = StrUtils.getComplexRandomString(4);
    redisTemplate.opsForValue().set(redisKey,randomCode,5, TimeUnit.MINUTES);
        OutputStream os = response.getOutputStream();
        String imgString = VerifyCodeUtils.verifyCode(115,40,randomCode);
        return imgString;
    }
}
